package com.example.flshtarvel.controller;
import com.example.flshtarvel.entity.History;
import com.example.flshtarvel.entity.PageResult;
import com.example.flshtarvel.entity.Route;
import com.example.flshtarvel.service.*;
import com.example.flshtarvel.vo.RouteVo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class RouteController {
    @Autowired
    RouteService routeService;
//    @Autowired
//    RouteLikeMapService routeLikeMapService;
    @Autowired
    LikeService likeService;
    @Autowired
    CollectService collectService;
    @Autowired
    HistoryService historyService;

    @PostMapping("/route")
    public Map<String,Object> insertRoute(RouteVo route){
        Map<String,Object> map =  new HashMap<String,Object>();
        int tag = routeService.insertRoute(route);
        if(tag==1){
            map.put("msg",1);
            map.put("route",route);
        }else{
            map.put("Msg",0);
        }
        return map;
    }

    @GetMapping("/route/{routeId}")
    public Map<String,Object> selectRouteById(@PathVariable("routeId")String routeId,
                                              HttpSession session){
        System.out.println("请求哦");
        Map<String,Object> map = new HashMap<String,Object>();
        RouteVo routeVoResult = routeService.selectRouteById(routeId);
        if(routeVoResult==null){
            map.put("msg",0);
        }else{
            //添加浏览记录
            History history = new History();
            history.setMessageId(routeId);
            history.setUserId((String)session.getAttribute("userId"));
            history.setArticleTypeId("04");
            history.setMessageName(routeVoResult.getRouteName());
            historyService.history(history);

            map.put("msg",1);
            //是否点赞
            int isLike = likeService.selectLikeByMessageIdAndUserId(routeId,(String)session.getAttribute("userId"));
            //int isLike = routeLikeMapService.selectRouteLikeByRouteIdAndUserId(routeId,userId);
            //是否收藏
            int isCollect = collectService.selectCollectByMessageIdAndUserId(routeId,(String)session.getAttribute("userId"));
            //int isCollect = routeCollectMapService.selectRouteCollectByRouteIdAndUserId(routeId,userId);
            map.put("route",routeVoResult);
            map.put("isLike",isLike);
            map.put("isCollect",isCollect);
        }
        return map;
    }

    @GetMapping("/route")
    public Map<String,Object> selectAllRoute(
                                             @RequestParam(name="page",defaultValue = "1") int page,
                                             @RequestParam(name="size",defaultValue = "10") int size){
        Map<String,Object> map = new HashMap<String,Object>();
        PageResult pageResult = new PageResult();
        List<RouteVo> routeList = routeService.selectAllRoute(page,size);
        if(routeList.isEmpty()){
            map.put("msg",0);
        }else{
            map.put("msg",1);
            map.put("routeList",routeList);
        }
        int count = routeService.selectRouteCount();
        pageResult.setCurrentPage(page);
        pageResult.setCount(count);
        pageResult.setPageSize(10);
        pageResult.setPageCount(count,size);

        map.put("pageResult",pageResult);
        return map;

    }

    @GetMapping("/route/selectRouteByName")
    public Map<String,Object> selectRouteByName(@RequestParam("routeName") String routeName){
        Map<String,Object> map = new HashMap<String,Object>();
        List<RouteVo> routeList = routeService.selectRouteByName(routeName);
        if(routeList.isEmpty()){
            map.put("msg",0);
        }else{
            map.put("msg",1);
            map.put("routeList",routeList);
        }
        return map;
    }

    //接收删除的链接文章id字符串
    @PostMapping("/editRoute")
    public Map<String,Object> updateRoute(Route route,String articleIds){
        Map<String,Object> map = new HashMap<String,Object>();
        int tag = routeService.updateRoute(route,articleIds);
        if(tag==1){
            map.put("msg",1);
            map.put("route",route);
        }else{
            map.put("msg",0);
        }
        return map;
    }

    @DeleteMapping("/deleteRoute/{routeId}")
    public Map<String,Object> deleteRoute(@PathVariable("routeId") String routeId){
        Map<String,Object> map = new HashMap<String,Object>();
        int tag = routeService.deleteRoute(routeId);
        if(tag==1){
            map.put("msg",1);
        }else{
            map.put("msg",0);
        }
        return map;
    }
    //首页搜索接口 路线
    @GetMapping("/search/route")
    public Map<String,Object> searchRoute(@RequestParam("search") String search){
        Map<String,Object> map = new HashMap<String,Object>();
        List<RouteVo> routeVoList = new ArrayList<RouteVo>();
        routeVoList = routeService.searchRoute(search);
        if(routeVoList.isEmpty()){
            map.put("msg",0);
        }else{
            map.put("msg",1);
            map.put("routeList",routeVoList);
        }
        return map;
    }

    //匹配路线  根据标签
    @GetMapping("/matchRouteByTag")
    public Map<String,Object> matchRouteByTag(@RequestParam("tagString")String tagString){
        Map<String,Object> map = new HashMap<>();
        List<RouteVo> routeVoList = routeService.matchRouteByTag(tagString);
        if(routeVoList.size()==0){
            map.put("msg",0);
        }else{
            map.put("msg",1);
            map.put("routeList",routeVoList);
        }
        return map;
    }
    //查询所有路线    根据时间排序
    @GetMapping("/seleteRouteByTime")
    public Map<String,Object> selectRouteByTime(  @RequestParam(name="page",defaultValue = "1") int page,
                                                  @RequestParam(name="size",defaultValue = "10") int size){
        Map<String,Object> map = new HashMap<>();
        List<RouteVo> routeVoList = new ArrayList<>();
        routeVoList = routeService.selectRouteByTime(page,size);
        if(routeVoList.size()==0){
            map.put("msg",0);
        }else{
            map.put("msg",1);
            map.put("routeList",routeVoList);
        }

        return map;
    }

    //查询所有路线    根据时间排序
    @GetMapping("/seleteRouteByHot")
    public Map<String,Object> selectRouteByHot(  @RequestParam(name="page",defaultValue = "1") int page,
                                                  @RequestParam(name="size",defaultValue = "10") int size){
        Map<String,Object> map = new HashMap<>();
        List<RouteVo> routeVoList = new ArrayList<>();
        routeVoList = routeService.selectRouteByHot(page,size);
        if(routeVoList.size()==0){
            map.put("msg",0);
        }else{
            map.put("msg",1);
            map.put("routeList",routeVoList);
        }

        return map;
    }
}
